package com.github.houkunlin.ui;

import com.github.houkunlin.config.ConfigService;
import com.github.houkunlin.model.DataTableModel;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.ui.EditorNotificationPanel;
import com.intellij.ui.components.panels.HorizontalLayout;

import javax.swing.*;
import java.awt.*;

/**
 *
 */
public class TemplateNotificationPanel extends EditorNotificationPanel{

    private static final Logger LOG = Logger.getInstance(TemplateNotificationPanel.class);

    private final DataTableModel dataTableModel;

    public TemplateNotificationPanel(DataTableModel dataTableModel, Project project){
        super();
        this.dataTableModel = dataTableModel;
        setText("Template setting");
        ConfigService configService = ConfigService.getInstance(project);
        JButton refreshGroupButton = new JButton("refresh");
        refreshGroupButton.addActionListener(e -> {
            FileDocumentManager.getInstance().saveAllDocuments();
            if(configService != null){
                configService.refresh();
            }
            dataTableModel.dataSourceModelUpdated();
        });
        myLinksPanel.add(HorizontalLayout.CENTER,refreshGroupButton);
        JLabel label = new JLabel("table:");
        myLinksPanel.add(HorizontalLayout.CENTER, label);

        myLinksPanel.add(HorizontalLayout.CENTER, dataTableModel.getDataSourceComboBoxPanel());
        JPanel panel = (JPanel) getComponent(0);
        panel.removeAll();
        panel.setLayout(new BorderLayout());
        panel.add(BorderLayout.WEST, myLabel);
        panel.add(BorderLayout.CENTER, myLinksPanel);
    }
}
